﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;

namespace Paltop_v0._1
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            Paltop_.Controllers.Schedule sh = new Paltop_.Controllers.Schedule();
            sh.createSchedule(DateTime.Now);
            Paltop_DatabaseLayer.PaltopDatabaseEntities db = new Paltop_DatabaseLayer.PaltopDatabaseEntities();
            if (db.EventTypes.Select(p => p).ToList().Count == 0)
            {
                var temp = new Paltop_DatabaseLayer.EventType { EventName = "Aniversiry", EventTypeID=1 };
                db.EventTypes.AddObject(temp);
                temp = new Paltop_DatabaseLayer.EventType { EventName = "BirthDay", EventTypeID=2 };
                db.EventTypes.AddObject(temp);
                temp = new Paltop_DatabaseLayer.EventType { EventName = "Appointment", EventTypeID = 3 };
                db.EventTypes.AddObject(temp);
                db.SaveChanges();
                Context.Application["onineUser"] = 0;
            }
            UserAgentXmlReader();
            //var setting = db.SYSMainSettings.Where(s => s.Title == "Extentions");
            //XElement Extentions = new XElement("Extentions");
            ////////////////////////////////////////jpg/////////////////////////////////////////////
            //XElement Extention = new XElement("Extention");
            //Extentions.Add(Extention);
            //XElement tempElem = new XElement("Title");
            //tempElem.Value = "jpg";
            //Extention.Add(tempElem);
            //tempElem = new XElement("JSFunction");
            //tempElem.Value = "OpenJpgFile(this.id)";
            //Extention.Add(tempElem);
            /////////////////////////////////////txt////////////////////////////////////////////////
            //Extention = new XElement("Extention");
            //Extentions.Add(Extention);
            //tempElem = new XElement("Title");
            //tempElem.Value = "txt";
            //Extention.Add(tempElem);
            //tempElem = new XElement("JSFunction");
            //tempElem.Value = "OpenTextFile(this.id)";
            //Extention.Add(tempElem);
            /////////////////////////////////////sql////////////////////////////////////////////////
            //Extention = new XElement("Extention");
            //Extentions.Add(Extention);
            //tempElem = new XElement("Title");
            //tempElem.Value = "sql";
            //Extention.Add(tempElem);
            //tempElem = new XElement("JSFunction");
            //tempElem.Value = "OpenTextFile(this.id)";
            //Extention.Add(tempElem);
            //if (setting.Count() == 0)
            //{
            //    Paltop_DatabaseLayer.SYSMainSetting sysmainSetting = new Paltop_DatabaseLayer.SYSMainSetting { ActiveFlag = true, Setting = Extentions.ToString(), Title = "Extention" };
            //    db.SaveChanges();
            //}
            //else
            //{
                
            //    if (setting.First().Setting != Extentions.ToString())
            //    {
            //        setting.First().Setting = Extentions.ToString();
            //        db.SaveChanges();
            //    }
            //}
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            UserAgentXmlReader();
            // Code that runs when a new session is started
            if (Context.Application["onineUser"] != null)
                Context.Application["onineUser"] = Convert.ToInt32(Context.Application["onineUser"]) + 1;
            else
                Context.Application["onineUser"] = 1;
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            if (Context.Application["onineUser"] != null)
                Context.Application["onineUser"] = Convert.ToInt32(Context.Application["onineUser"]) - 1;
            else
                Context.Application["onineUser"] = 0;
        }

        void UserAgentXmlReader()
        {
            XDocument osType= XDocument.Load(Server.MapPath("/Doc/OsType.xml"));
            XDocument  browserType= XDocument.Load(Server.MapPath("/Doc/BrowserTyp.xml"));
            XDocument resType = XDocument.Load(Server.MapPath("/Doc/resolution.xml"));

            var oses = (from p in osType.Descendants("os")
                          select new Paltop_v0._1.Entities.Os
                          {
                              OSID = Convert.ToInt32(p.Element("id").Value),
                              OSName = p.Element("name").Value.ToLower()
                          }).ToList();
            var browsers = (from p in browserType.Descendants("browser")
                        select new Paltop_v0._1.Entities.Browser
                        {
                            BrowserID = Convert.ToInt32(p.Element("id").Value),
                            BrowserName = p.Element("name").Value.ToLower()
                        }).ToList();
            var reses = (from p in resType.Descendants("resolution")
                            select new Paltop_v0._1.Entities.Resolution
                            {
                                ResolutionID = Convert.ToInt32(p.Element("id").Value),
                                Height=Convert.ToInt32(p.Element("height").Value),
                                Width=Convert.ToInt32(p.Element("width").Value),
                                ResoluationName = p.Element("name").Value.ToLower()
                            }).ToList();
            Context.Application["resTypes"] = reses;
            Context.Application["osTypes"] = oses;
            Context.Application["browserTypes"] = browsers;
        }

    }

}
